import co
import co.mutex as m
import time


fn main() {
    int start = time.unix()
    int result_no_lock = 0
    var sum_no_lock = fn() {
        for int i = 0; i < 100; i+=1 {
            result_no_lock += 1
        }
    }

    for int i = 0; i < 10000; i+=1 {
        go sum_no_lock()
    }

    // 等待 1s 让协程完成
    co.sleep(1000)
    println("post result_no_lock completed", result_no_lock, time.unix() - start)
    assert(result_no_lock < 1000000)

    var mu = m.mutex_t{}
    int result_lock = 0

    var sum_lock = fn() {
        mu.lock()
        for int i = 0; i < 100; i+=1 {
            result_lock += 1
        }
        mu.unlock()
    }

    start = time.unix()
    for int i = 0; i < 5000; i+=1 {
        go sum_lock()
    }
    println('sum_lock coroutine create success, use time = ', time.unix() - start)

    // wait completed
    for int i = 0; i <= 100; i+=1 {
        if result_lock == 500000 {
            break
        }
        println('wait result lock complete..., current =', result_lock)
        co.sleep(200)
    }

    println("post result_lock: ", &result_lock, result_lock)

    assert(result_lock == 500000)
}